package b1.mobile.blackberry.screen;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.io.http.Serviceworker.ServiceDelegate;
import b1.mobile.blackberry.res.B1resourceResource;
import b1.mobile.blackberry.ui.component.util.ProgressAnimationField;

public class BaseFunctionScreen extends MainScreen implements B1resourceResource, ServiceDelegate{

	protected static ResourceBundle resources = ResourceBundle.getBundle(
			BUNDLE_ID, BUNDLE_NAME);

	private ProgressAnimationField loadingIcon;
	protected VerticalFieldManager footFieldManager;

	public BaseFunctionScreen() {
		super();
		footFieldManager = new VerticalFieldManager();

		this.setStatus(footFieldManager);
	}

	public BaseFunctionScreen(long style) {
		super(style);
		footFieldManager = new VerticalFieldManager();
		this.setStatus(footFieldManager);
	}

	protected void onLoading() {
		if (loadingIcon == null) {
			loadingIcon = new ProgressAnimationField(Bitmap.getBitmapResource("spinner.png"), 5,
					Field.FIELD_HCENTER);
			footFieldManager.insert(loadingIcon, 0);
		}
	}

	protected void onLoadingDone() {
		footFieldManager.delete(loadingIcon);
	}

	public void onGetResult(Serviceworker worker) {
		if(worker.responseCode == 401){
			UiApplication.getUiApplication().invokeLater(new Runnable(){
				public void run() {
					onTimeout();
				}
			});
		}
	}
	
	private void onTimeout() {
		WelcomeScreen welcome = new WelcomeScreen();
		welcome.timeOut = true;
		UiApplication.getUiApplication().pushScreen(welcome);
	}

	public void onDataReady(Serviceworker worker, Object data) {
		
	}

}
